package datastructure.sort;

import java.util.Comparator;

/**
 * Description：选择排序
 * 即每次选择一个最大或最小的元素，放到最终位置上
 * 优化：每次从已经确定的后一个元素开始比较
 * Author: zhangc
 * Date：2017/6/19 21:13
 */
public final class SelectSort {
    public static <T extends Comparable<T>> void selectSortAble(T[] array) {
        T temp;
        int index;
        for (int i = 0; i < array.length; ++i) {
            index = i;
            for (int j = i + 1; j < array.length; ++j) {
                if (array[ j ].compareTo(array[ index ]) > 0) {
                    index = j;
                }
            }
            temp = array[ i ];
            array[ i ] = array[ index ];
            array[ index ] = temp;
        }
    }


    public static <T> void selectSortOr(T[] array, Comparator<T> comparor) {
        T temp;
        int index;
        for (int i = 0; i < array.length; ++i) {
            index = i;
            for (int j = i + 1; j < array.length; ++j) {
                if (comparor.compare(array[ index ], array[ j ]) > 0) {
                    index = j;
                }
            }
            temp = array[ i ];
            array[ i ] = array[ index ];
            array[ index ] = temp;
        }
    }
}
